package com.example.demo.controller;

import com.example.demo.service.PrintService;
import com.example.demo.vo.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 打印接口controller
 *
 * @author nrz
 * @date 2024/8/31
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(value = "/print")
public class PrintController {

    @Autowired
    private PrintService printService;

    /**
     * 打印数据写入json文件
     * @param projectName 项目名称
     * @param printName 打印数据标识
     * @param printData 数据内容
     * @return 返回值
     */
    @RequestMapping(value = "/{projectName}/{printName}/savePrintData",
            method = RequestMethod.POST, produces = "application/json")
    public RestResponse savePrintData(@PathVariable(name = "projectName") String projectName,
                                @PathVariable(name = "printName") String printName,
                                @RequestBody Map<String, Object> printData) {
        try {
            Map<String, Object> data = printService.savePrintData(projectName, printName, printData);
            return new RestResponse.DataResponse<>(RestResponse.ResponseType.SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new RestResponse.DataResponse<>(RestResponse.ResponseType.ERROR, "打印数据写入失败");
        }
    }

    /**
     * 获取打印模板和数据
     *
     * @param projectName 项目名称
     * @param configName 打印模板标识
     * @param printName 打印数据标识
     * @return 打印模板和数据
     */
    @RequestMapping(value = "/{projectName}/{configName}/{printName}/getPrintConfigAndData", method = RequestMethod.GET)
    public RestResponse getPrintConfigAndData(@PathVariable(name = "projectName") String projectName,
                                      @PathVariable(name = "configName") String configName,
                                      @PathVariable(name = "printName") String printName) {
        try {
            Map<String, Object> data = printService.getPrintConfigAndData(projectName, configName, printName);
            return new RestResponse.DataResponse<>(RestResponse.ResponseType.SUCCESS, data);
        } catch (Exception e) {
            return new RestResponse.DataResponse<>(RestResponse.ResponseType.ERROR, "打印数据获取失败");
        }
    }

}
